export default {
  addCart(context, payload) {
    // 1、playLoad新添加的商品
    /*let index = state.cartList.indexOf(payload)
    // 2、判断oldProduct
    if (index !== -1) {
      let oldProduct = state.cartList[index]
      oldProduct.count += 1
    }else {
      payload.count = 1
      state.cartList.push(payload)
    }*/
    // 查找之前数组中是否有该商品
    return new Promise((resolve, reject) => {
      let oldProduct = context.state.cartList.find(item => {
        return item.iid === payload.iid
      })
      // 2、判断oldProduct
      if (oldProduct) {
        context.commit('addCounter', oldProduct)
        resolve('当前商品数量 + 1')
      }else {
        payload.count = 1
        // context.state.cartList.push(payload)
        context.commit('addToCart', payload)
        resolve('添加了新的商品')
      }
    })
  }
}
